﻿<?php

include("inc_configuration.php");

$tID=0;
$et=0;
$gt=0;
$pk=0;
$sp=0;
$anz=0;


$lId=$liga;
	




	$gnq="SELECT groupName FROM $tbl_lg WHERE id=$gruppe";
	$gnqe=mysql_query($gnq);
	$gnqeo=mysql_fetch_object($gnqe);

	$gname=$gnqeo->groupName;
	$gId=$gruppe;

	echo "<h1>$gname</h1>";
	$tQ = "SELECT t.id FROM $tbl_teams t, $tbl_gt gt WHERE gt.groupId=$gId AND gt.teamId=t.id";
	$tQe = mysql_query($tQ);
	
	$team = array();
	$teams = array();
	
	while ($row = mysql_fetch_object($tQe))
	{
		$tID=$row->id;
		
		$team = array(
				  tID => $tID,
				  et => 0,
				  gt => 0,
				  sp => 0,
				  s => 0,
				  n => 0,
				  u => 0);
		
		$teams[] = $team;
		$anz++;
	}

	$mQ = "SELECT * FROM $tbl_matches WHERE leagueId=$lId AND stageId=$gId AND free>=3 AND matchday<($matchday+1)";
	$mQe = mysql_query($mQ);
	

	while ($row = mysql_fetch_object($mQe))
	{
		
		$hT = $row->homeT;
		$gT = $row->guestT;
		$count=0;
		for($count = 0; $count < $anz; $count++)
			{
				$tI=$teams[$count][tID];

				if (($hT==$tI) OR ($gT==$tI))
				{
					$aT=$teams[$count];
					$hG = $row->homeG;
					$gG = $row->guestG;
					$wi = $row->winner;
					
					$aT[sp]=$aT[sp]+1;
					if ($hT==$tI)
					{
						$aT[et]= $aT[et]+$hG;
						$aT[gt]= $aT[gt]+$gG;
						if ($wi==1)
						{
							$aT[s]=$aT[s]+1;
						}
						else
						{
							if ($wi==2)
							{
								$aT[n]=$aT[n]+1;
							}
							else
							{
								$aT[u]=$aT[u]+1;
							}
						}
					}
					else
					{
						if ($gT==$tI)
						{
							$aT[et] = $aT[et]+$gG;
							$aT[gt] = $aT[gt]+$hG;
							if ($wi==1)
							{
								$aT[n]=$aT[n]+1;
							}
							else
							{
								if ($wi==2)
								{
									$aT[s]=$aT[s]+1;
								}
								else
								{
									$aT[u]=$aT[u]+1;
								}
							}
						}
					}
					$teams[$count]=$aT;
				}
			}
	}

	$count=0;
	for($count = 0; $count < $anz; $count++)
	{
		$tA=$teams[$count];
		$userId=$jUser;
		$teamId="$tA[tID]";
		$et="$tA[et]";
		$gt="$tA[gt]";
		$td=$et-$gt;
		$s="$tA[s]";
		$u="$tA[u]";
		$n="$tA[n]";
		$pkt=($s*3)+($u);
		$sp="$tA[sp]";
		
		$tableQ="INSERT INTO 	$tbl_table (userId,teamId,sp,s,u,n,et,gt,td,pkt) 
								VALUES ($userId,$teamId,$sp,$s,$u,$n,$et,$gt,$td,$pkt)";
		$tableQe=mysql_query($tableQ);
		
		
	}


	$tsQ="	SELECT t.id, t.name, lt.sp,lt.s,lt.u,lt.n,lt.et,lt.gt,lt.td,lt.pkt 
			FROM $tbl_table lt, $tbl_teams t 
			WHERE
			lt.teamId=t.id AND
			lt.userId=$jUser
			ORDER BY pkt DESC,td DESC,et DESC";
	$tsQe=mysql_query($tsQ);

	echo "<table id='tborder' bgcolor='#DADADA'>";

	echo "	<thead id='tabelle'><tr>
						<th>Platzierung</th>
						<th/>
						<th>Mannschaft</th>
						<th>Trainer</th>
						<th>Spiele</th>
						<th>S</th>
						<th>U</th>
						<th>N</th>
						<th>T</th>
						<th>GT</th>
						<th>TD</th>
						<th>Punkte</th>
					</tr></thead>";
				

	$p=1;

	while ($row = mysql_fetch_object($tsQe))
	{

		$tI = $row->id;

		$uQ="	SELECT username AS uGn, c.id AS gC, u.id AS uid FROM $tbl_combinations c, $tbl_users u
				WHERE
				(c.teamId=$tI) AND
				(c.active=1) AND
				(c.userId=u.id)";
				
		$uQe = mysql_query($uQ);

		$uQc = mySQL_num_rows($uQe);
		
		if ($uQc==0)
		{
			$cN="(-)";
		}
		else
		{
			$cNq = mysql_fetch_object($uQe);
			$cN=$cNq->uGn;
			$uid=$cNq->uid;
			$cN = "<a href='profil.php?id=$uid'>$cN</a>";
		}
		
		$tN=$row->name;
		$et=$row->et;
		$gt=$row->gt;
		$td=$row->td;
		$s=$row->s;
		$u=$row->u;
		$n=$row->n;
		$pkt=$row->pkt;
		$sp=$row->sp;
		
		if ($td>0)
		{
			$tdz=$td;
			$td="+$tdz";
		}
		
		
		$color="";
		
		$q = "SELECT *
				FROM
					$tbl_rankings
				WHERE
					(leagueId=$lId) AND
					($p>=start) AND
					($p<=end)";
					
	
	
		$qe = mysql_query($q);
		
		$qec = mysql_num_rows($qe);
		
		if ($qec>0)
		{
			$qeo = mysql_fetch_object($qe);
			$ccode=$qeo->color;
			$color="bgcolor='$ccode'";
		}
		
		
		echo "	<tr $color>
					<td>$p.</td>
					<td><a href='team_kader.php?team=$tI'><img src='img/logos/",$tI,".png' width='35' height='35' alt='' align='middle'></a></td>
					<td><a href='spiele_team.php?team=$tI&liga=$lId'>$tN</a></td>
					<td>$cN</td>
					<td>$sp</td>
					<td>$s</td>
					<td>$u</td>
					<td>$n</td>
					<td>$et</td>
					<td>$gt</td>
					<td>$td</td>
					<td>$pkt</td>
				</tr>";
		
		
		$p++;
	}
	
	echo "<tr><th colspan='12'></th></tr></table><br>";
	
	$l = "DELETE FROM $tbl_table WHERE userId=$jUser";
	$le = mysql_query($l);
	




?>